package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements  SetmealService {
    @Autowired
    SetmealDao setmealDao;
    @Autowired
    CheckGroupDao checkGroupDao;


    @Override
    public void add(Setmeal setmeal) {
        setmealDao.add(setmeal);
        Integer setMealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if (CollectionUtil.isNotEmpty(checkgroupIds)) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setSetMealAndCheckGroupRelation(setMealId, checkgroupId);
            }
        }

//        setSetMealAndCheckGroupRelation(setMealId,setmeal.getCheckgroupIds());
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> setmealPage = setmealDao.findPage(queryPageBean.getQueryString());
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmealList = setmealDao.getSetmeal();
        return setmealList;
    }


    //    public void setSetMealAndCheckGroupRelation(Integer setMealId, List<Integer> checkGroupIds){
//       List<Map>  maps=new ArrayList<>();
//        for (Integer checkGroupId : checkGroupIds) {
//                Map map=new HashMap();
//                map.put("setmeal_id",setMealId);
//                map.put("checkgroup_id",checkGroupId);
//                maps.add(map);
//        }
//        setmealDao.setSetMealAndCheckGroupRelationBatch(maps);
//    }


    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        if (setmeal != null) {
            Integer setmealId = setmeal.getId();
           List<CheckGroup>  checkGroups = setmealDao.findCheckGroupsBySetMealId(setmealId);
              if (CollectionUtil.isNotEmpty(checkGroups)){
                  for (CheckGroup checkGroup : checkGroups) {
                    List<CheckItem> checkItems =setmealDao.findCheckItemByCheckGroup(checkGroup.getId());
                    checkGroup.setCheckItems(checkItems);
                  }
              }
              setmeal.setCheckGroups(checkGroups);
        }
            return setmeal;
        }
    }
